package antlr4.definitive.d4_expr.d4_4_2_data.run;

import antlr4.definitive.d4_expr.d4_3_0_extractinterface.gen.JavaLexer;
import antlr4.definitive.d4_expr.d4_3_0_extractinterface.gen.JavaParser;
import antlr4.definitive.d4_expr.d4_4_2_data.gen.DataLexer;
import antlr4.definitive.d4_expr.d4_4_2_data.gen.DataParser;
import cn.hutool.core.io.FileUtil;
import org.antlr.v4.runtime.CharStreams;
import org.antlr.v4.runtime.CodePointCharStream;
import org.antlr.v4.runtime.CommonTokenStream;


public class Data {

    public static void main(String[] args) {
        if (args.length > 0) {
            String arg = args[0];
            String content = FileUtil.readString(arg, "UTF-8");
            System.out.println("读取文件" + arg + ",内容:\n" + content);
            CodePointCharStream input = CharStreams.fromString(content);
            System.out.println("开始解析文件");

//            System.out.println("len:"+ $n + ",i:"+ $i + ",val:" + $INT.int);
//            System.out.println(String.format("len:%s,i:%s,val:%s",$n,$i,$INT.int));

            JavaLexer lexer = new JavaLexer(input);
            CommonTokenStream tokens = new CommonTokenStream(lexer);


//            List<? extends Token> allTokens = lexer.getAllTokens();
//            for (Token token : allTokens) {
//                System.out.println(token.getType() + " text:" + token.getText());
//            }
//            System.out.println(allTokens);   //用于查看token识别的是否正确，文本类型的建议使用文本编辑器修改，而不是用idea，idea的Tab好像识别不到。

            DataParser parser = new DataParser(tokens);
            parser.setBuildParseTree(false);//不需要浪费时间建立语法分析树
            parser.file();//开始语法分析

        } else {
            System.err.println("不推荐手写字符串，还是读取一个已经存在的文件吧");
        }
    }

}
